<html lang="zh-CN"><head><meta charset="UTF-8"><style>.nodata  main {width:1000px;margin: auto;}</style></head><body class="nodata " style=""><div class="main_father clearfix d-flex justify-content-center " style="height:100%;"> <div class="container clearfix " id="mainBox"><main><div class="blog-content-box">
<div class="article-header-box">
<div class="article-header">
<div class="article-title-box">
<h1 class="title-article" id="articleContentId">(C卷,200分)- 可以组成网络的服务器（Java & JS & Python & C）</h1>
</div>
</div>
</div>
<div id="blogHuaweiyunAdvert"></div>

        <div id="article_content" class="article_content clearfix">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/kdoc_html_views-1a98987dfd.css">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/ck_htmledit_views-044f2cf1dc.css">
                <div id="content_views" class="htmledit_views">
                    <h4 id="main-toc">题目描述</h4> 
<p>在一个机房中&#xff0c;服务器的位置标识在 n*m 的整数矩阵网格中&#xff0c;1 表示单元格上有服务器&#xff0c;0 表示没有。如果两台服务器位于同一行或者同一列中紧邻的位置&#xff0c;则认为它们之间可以组成一个局域网。</p> 
<p>请你统计机房中最大的局域网包含的服务器个数。</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%85%A5%E6%8F%8F%E8%BF%B0">输入描述</h4> 
<p>第一行输入两个正整数&#xff0c;n和m&#xff0c;0&lt;n,m&lt;&#61;100</p> 
<p>之后为n*m的二维数组&#xff0c;代表服务器信息</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%87%BA%E6%8F%8F%E8%BF%B0">输出描述</h4> 
<p>最大局域网包含的服务器个数。</p> 
<p></p> 
<h4 id="%E7%94%A8%E4%BE%8B">用例</h4> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:86px;">输入</td><td style="width:412px;">2 2<br /> 1 0<br /> 1 1</td></tr><tr><td style="width:86px;">输出</td><td style="width:412px;">3</td></tr><tr><td style="width:86px;">说明</td><td style="width:412px;">[0][0]、[1][0]、[1][1]三台服务器相互连接&#xff0c;可以组成局域网</td></tr></tbody></table> 
<p></p> 
<h4 id="%E9%A2%98%E7%9B%AE%E8%A7%A3%E6%9E%90">题目解析</h4> 
<p>本题可以用深度优先搜索DFS求解。</p> 
<p>我们找到一个服务器后&#xff0c;就再去其上下左右找下一个服务器&#xff0c;当找到新服务器&#xff0c;再递归去找其上下左右&#xff0c;按此逻辑&#xff0c;就像拔地瓜藤一样&#xff0c;一下子把所有地瓜都拔出来。而这就是深度优先搜索&#xff0c;即dfs。</p> 
<p>为了避免重复统计&#xff0c;我们将统计过的服务器位置的值从1变为0。</p> 
<hr /> 
<p>2023.06.18</p> 
<p>本题极限用例下&#xff0c;深度优先搜索&#xff08;递归实现&#xff09;可能会发生Stack Overflow&#xff0c;因此推荐使用广度优先搜索&#xff0c;广度优先搜索依赖于自定义的队列&#xff0c;来完成图的遍历&#xff0c;因此不会发生Stack Overflow。</p> 
<p>关于广度优先搜索可以看&#xff1a;</p> 
<p><a href="https://fcqian.blog.csdn.net/article/details/127711317" rel="nofollow" title="华为OD机试 - 计算疫情扩散时间&#xff08;Java &amp; JS &amp; Python&#xff09;_华为机考数组扩散求用时_伏城之外的博客-CSDN博客">华为OD机试 - 计算疫情扩散时间&#xff08;Java &amp; JS &amp; Python&#xff09;_华为机考数组扩散求用时_伏城之外的博客-CSDN博客</a></p> 
<p>当然&#xff0c;本题也可以基于深度优先搜索&#xff08;自定义栈&#xff09;实现&#xff0c;对应代码可以参考&#xff1a;</p> 
<p><a href="https://fcqian.blog.csdn.net/article/details/130774841" rel="nofollow" title="华为OD机试 - 寻找最大价值的矿堆&#xff08;Java &amp; JS &amp; Python&#xff09;_伏城之外的博客-CSDN博客">华为OD机试 - 寻找最大价值的矿堆&#xff08;Java &amp; JS &amp; Python&#xff09;_伏城之外的博客-CSDN博客</a></p> 
<p></p> 
<h4>JavaScript算法源码</h4> 
<pre><code class="language-javascript">/* JavaScript Node ACM模式 控制台输入获取 */
const readline &#61; require(&#34;readline&#34;);

const rl &#61; readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

const lines &#61; [];
let n, m;
let matrix;
rl.on(&#34;line&#34;, (line) &#61;&gt; {
  lines.push(line);

  if (lines.length &#61;&#61;&#61; 1) {
    [n, m] &#61; lines[0].split(&#34; &#34;).map(Number);
  }

  if (n &amp;&amp; lines.length &#61;&#61;&#61; n &#43; 1) {
    lines.shift();
    matrix &#61; lines.map((line) &#61;&gt; line.split(&#34; &#34;).map(Number));

    console.log(getResult());

    lines.length &#61; 0;
  }
});

function getResult() {
  let ans &#61; 0;
  for (let i &#61; 0; i &lt; n; i&#43;&#43;) {
    for (let j &#61; 0; j &lt; m; j&#43;&#43;) {
      if (matrix[i][j] &#61;&#61; 1) ans &#61; Math.max(ans, bfs(i, j));
    }
  }

  return ans;
}

const offsets &#61; [
  [-1, 0],
  [1, 0],
  [0, -1],
  [0, 1],
];

function bfs(i, j) {
  let count &#61; 1;
  matrix[i][j] &#61; 0;

  const queue &#61; [[i, j]];

  while (queue.length &gt; 0) {
    const [x, y] &#61; queue.shift();

    for (let [offsetX, offsetY] of offsets) {
      const newX &#61; x &#43; offsetX;
      const newY &#61; y &#43; offsetY;

      if (
        newX &gt;&#61; 0 &amp;&amp;
        newX &lt; n &amp;&amp;
        newY &gt;&#61; 0 &amp;&amp;
        newY &lt; m &amp;&amp;
        matrix[newX][newY] &#61;&#61; 1
      ) {
        count&#43;&#43;;
        matrix[newX][newY] &#61; 0;
        queue.push([newX, newY]);
      }
    }
  }

  return count;
}
</code></pre> 
<p></p> 
<h4>Java算法源码</h4> 
<pre><code class="language-java">import java.util.LinkedList;
import java.util.Scanner;

public class Main {
  static int n;
  static int m;
  static int[][] matrix;

  public static void main(String[] args) {
    Scanner sc &#61; new Scanner(System.in);

    n &#61; sc.nextInt();
    m &#61; sc.nextInt();

    matrix &#61; new int[n][m];
    for (int i &#61; 0; i &lt; n; i&#43;&#43;) {
      for (int j &#61; 0; j &lt; m; j&#43;&#43;) {
        matrix[i][j] &#61; sc.nextInt();
      }
    }

    System.out.println(getResult());
  }

  public static int getResult() {
    int ans &#61; 0;
    for (int i &#61; 0; i &lt; n; i&#43;&#43;) {
      for (int j &#61; 0; j &lt; m; j&#43;&#43;) {
        if (matrix[i][j] &#61;&#61; 1) ans &#61; Math.max(ans, bfs(i, j));
      }
    }

    return ans;
  }

  static int[][] offsets &#61; {<!-- -->{-1, 0}, {1, 0}, {0, -1}, {0, 1}};

  public static int bfs(int i, int j) {
    LinkedList&lt;int[]&gt; queue &#61; new LinkedList&lt;&gt;();

    int count &#61; 1;
    matrix[i][j] &#61; 0;

    queue.add(new int[] {i, j});

    while (queue.size() &gt; 0) {
      int[] pos &#61; queue.removeFirst();

      int x &#61; pos[0];
      int y &#61; pos[1];

      for (int[] offset : offsets) {
        int newX &#61; x &#43; offset[0];
        int newY &#61; y &#43; offset[1];

        if (newX &gt;&#61; 0 &amp;&amp; newX &lt; n &amp;&amp; newY &gt;&#61; 0 &amp;&amp; newY &lt; m &amp;&amp; matrix[newX][newY] &#61;&#61; 1) {
          count&#43;&#43;;
          matrix[newX][newY] &#61; 0;
          queue.add(new int[] {newX, newY});
        }
      }
    }

    return count;
  }
}
</code></pre> 
<p></p> 
<h4>Python算法源码</h4> 
<pre><code class="language-python"># 输入获取
n, m &#61; map(int, input().split())
matrix &#61; [list(map(int, input().split())) for _ in range(n)]

offsets &#61; ((-1, 0), (1, 0), (0, -1), (0, 1))


def bfs(i, j):
    count &#61; 1
    matrix[i][j] &#61; 0

    queue &#61; [[i, j]]

    while len(queue) &gt; 0:
        x, y &#61; queue.pop(0)

        for offsetX, offsetY in offsets:
            newX &#61; x &#43; offsetX
            newY &#61; y &#43; offsetY

            if n &gt; newX &gt;&#61; 0 and m &gt; newY &gt;&#61; 0 and matrix[newX][newY] &#61;&#61; 1:
                count &#43;&#61; 1
                matrix[newX][newY] &#61; 0
                queue.append([newX, newY])

    return count


# 算法入口
def getResult():
    ans &#61; 0

    for i in range(n):
        for j in range(m):
            if matrix[i][j] &#61;&#61; 1:
                ans &#61; max(ans, bfs(i, j))

    return ans


# 算法调用
print(getResult())
</code></pre> 
<p></p> 
<h4>C算法源码</h4> 
<pre><code class="language-cpp">#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;math.h&gt;

#define MAX_SIZE 100

typedef struct ListNode {
    int ele;
    struct ListNode *prev;
    struct ListNode *next;
} ListNode;

typedef struct {
    int size;
    ListNode *head;
    ListNode *tail;
} LinkedList;

LinkedList *new_LinkedList() {
    LinkedList *link &#61; (LinkedList *) malloc(sizeof(LinkedList));
    link-&gt;size &#61; 0;
    link-&gt;head &#61; NULL;
    link-&gt;tail &#61; NULL;
    return link;
}

void addLast_LinkedList(LinkedList *link, int ele) {
    ListNode *node &#61; (ListNode *) malloc(sizeof(ListNode));
    node-&gt;ele &#61; ele;
    node-&gt;prev &#61; NULL;
    node-&gt;next &#61; NULL;

    if (link-&gt;size &#61;&#61; 0) {
        link-&gt;head &#61; node;
        link-&gt;tail &#61; node;
    } else {
        link-&gt;tail-&gt;next &#61; node;
        node-&gt;prev &#61; link-&gt;tail;
        link-&gt;tail &#61; node;
    }

    link-&gt;size&#43;&#43;;
}

int removeFirst_LinkedList(LinkedList *link) {
    if (link-&gt;size &#61;&#61; 0) exit(-1);

    ListNode *removed &#61; link-&gt;head;

    if (link-&gt;size &#61;&#61; 1) {
        link-&gt;head &#61; NULL;
        link-&gt;tail &#61; NULL;
    } else {
        link-&gt;head &#61; link-&gt;head-&gt;next;
        link-&gt;head-&gt;prev &#61; NULL;
    }

    link-&gt;size--;

    int res &#61; removed-&gt;ele;

    free(removed);

    return res;
}

int offsets[4][2] &#61; {<!-- -->{-1, 0},
                     {1,  0},
                     {0,  -1},
                     {0,  1}};

int n, m;
int matrix[MAX_SIZE][MAX_SIZE];

int bfs(int i, int j);

int main() {
    scanf(&#34;%d %d&#34;, &amp;n, &amp;m);

    for (int i &#61; 0; i &lt; n; i&#43;&#43;) {
        for (int j &#61; 0; j &lt; m; j&#43;&#43;) {
            scanf(&#34;%d&#34;, &amp;matrix[i][j]);
        }
    }

    int res &#61; 0;

    for (int i &#61; 0; i &lt; n; i&#43;&#43;) {
        for (int j &#61; 0; j &lt; m; j&#43;&#43;) {
            if (matrix[i][j] &#61;&#61; 1) {
                res &#61; (int) fmax(res, bfs(i, j));
            }
        }
    }

    printf(&#34;%d\n&#34;, res);
}

int bfs(int i, int j) {
    LinkedList *queue &#61; new_LinkedList();

    int count &#61; 1;
    matrix[i][j] &#61; 0;

    addLast_LinkedList(queue, i * m &#43; j);

    while (queue-&gt;size &gt; 0) {
        int pos &#61; removeFirst_LinkedList(queue);

        int x &#61; pos / m;
        int y &#61; pos % m;

        for (int k &#61; 0; k &lt; 4; k&#43;&#43;) {
            int newX &#61; x &#43; offsets[k][0];
            int newY &#61; y &#43; offsets[k][1];

            if (newX &gt;&#61; 0 &amp;&amp; newX &lt; n &amp;&amp; newY &gt;&#61; 0 &amp;&amp; newY &lt; m &amp;&amp; matrix[newX][newY] &#61;&#61; 1) {
                count&#43;&#43;;
                matrix[newX][newY] &#61; 0;
                addLast_LinkedList(queue, newX * m &#43; newY);
            }
        }
    }

    return count;
}</code></pre> 
<p></p>
                </div>
        </div>
        <div id="treeSkill"></div>
        <div id="blogExtensionBox" style="width:400px;margin:auto;margin-top:12px" class="blog-extension-box"></div>
    <script>
  $(function() {
    setTimeout(function () {
      var mathcodeList = document.querySelectorAll('.htmledit_views img.mathcode');
      if (mathcodeList.length > 0) {
        for (let i = 0; i < mathcodeList.length; i++) {
          if (mathcodeList[i].naturalWidth === 0 || mathcodeList[i].naturalHeight === 0) {
            var alt = mathcodeList[i].alt;
            alt = '\\(' + alt + '\\)';
            var curSpan = $('<span class="img-codecogs"></span>');
            curSpan.text(alt);
            $(mathcodeList[i]).before(curSpan);
            $(mathcodeList[i]).remove();
          }
        }
        MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
      }
    }, 1000)
  });
</script>
</div></html>